/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mx.im.sinocab.dao;

import com.mx.im.sinocab.bean.Employ;
import com.mx.im.sinocab.bean.Menu;
import com.mx.im.sinocab.bean.Option;
import com.mx.im.sinocab.bean.User;
import com.mx.im.sinocab.bean.Usertype;
import com.mx.im.sinocab.util.Conection;
import com.mx.im.sinocab.util.Constant;
import static com.mx.im.sinocab.util.Constant.SQL_CONSULT_EMPLOY;
import static com.mx.im.sinocab.util.Constant.SQL_CONSULT_EMPLOY_WHEREID;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Tevez
 */
public class DaoEmploy implements Constant {

    private PreparedStatement pt;

    public boolean InsertarEmpleado(Employ unEmpleado) throws SQLException {

        boolean insertOK = false;



        Conection cone = new Conection();

        try {

            pt = cone.getConection().prepareStatement(SQL_INSERT_EMPLOY);

            pt.setString(1, unEmpleado.getName());
            pt.setString(2, unEmpleado.getLastName());
            pt.setString(3, unEmpleado.getPhone());
            pt.setString(4, unEmpleado.getCellphone());
            pt.setString(5, unEmpleado.getAddress());
            pt.setString(6, unEmpleado.getGender());
            pt.setDate(7, (java.sql.Date) unEmpleado.getBirthday());
            pt.setString(8, unEmpleado.getAge());
            pt.setString(9, unEmpleado.getEmail());
            pt.setDate(10, (java.sql.Date) unEmpleado.getAdmissionDate());
            pt.setString(11, unEmpleado.getTimeLaboring());
            pt.setString(12, unEmpleado.getDependentEconomic());
            pt.setString(13, unEmpleado.getWorkedTipe());
            pt.setString(14, unEmpleado.getMaritalStatus());
            pt.setString(15, unEmpleado.getStudies());

            insertOK = pt.executeUpdate() == 1;
            pt.close();
            cone.closeConection();

            //pt.setString(16, unEmpleado.getPhotography());
            //pt.setString(17, unEmpleado.getFingerPrint());       

        } catch (NullPointerException npe) {
            System.out.println("DaoEmploy - InsertarEmpleado(NPE): " + npe.getMessage());
            npe.printStackTrace();

        }

        return insertOK;

    }

    public List listEmpleados() {
        List listaEmpleados = new ArrayList();

        try {

            Conection cone = new Conection();

            pt = cone.getConection().prepareStatement(SQL_CONSULT_EMPLOY);
            ResultSet rs = pt.executeQuery();

            while (rs.next()) {

                JOptionPane.showMessageDialog(null, "Dentro de la consulta de registro");
                
                Employ em = new Employ();
                em.setIdEmploy(rs.getInt(1));
                em.setName(rs.getString(2));
                em.setLastName(rs.getString(3));
                em.setPhone(rs.getString(4));
                em.setCellphone(rs.getString(5));
                em.setAddress(rs.getString(6));
                em.setGender(rs.getString(7));
                em.setBirthday(rs.getDate(8));
                em.setAge(rs.getString(9));
                em.setMaritalStatus(rs.getString(10));
                em.setAdmissionDate(rs.getDate(11));
                em.setTimeLaboring(rs.getString(12));
                em.setEmail(rs.getString(13));
                em.setWorkedTipe(rs.getString(14));
                em.setDependentEconomic(rs.getString(15));
                em.setStudies(rs.getString(16));

                listaEmpleados.add(em);
            }
            rs.close();
            pt.close();
            cone.closeConection();

        } catch (SQLException e) {
            System.out.println("DAOEmpleado - listEmpleados() : "
                    + e.getMessage());
        }
        return listaEmpleados;
    }
    
    public Employ getEmpleado(int idEmploy){
        Employ em = new Employ();
      String selectSQL= "SELECT * FROM employ WHERE idEmploy ="+idEmploy+ ";";
     
        try{
            Conection cone = new Conection();
            pt = cone.getConection().prepareStatement(selectSQL);
            ResultSet rs = pt.executeQuery();
            
            rs.last();
            em.setIdEmploy(rs.getInt(1));
                em.setName(rs.getString(2));
                em.setLastName(rs.getString(3));
                em.setPhone(rs.getString(4));
                em.setCellphone(rs.getString(5));
                em.setAddress(rs.getString(6));
                em.setGender(rs.getString(7));
                em.setBirthday(rs.getDate(8));
                em.setAge(rs.getString(9));
                em.setMaritalStatus(rs.getString(10));
                em.setAdmissionDate(rs.getDate(11));
                em.setTimeLaboring(rs.getString(12));
                em.setEmail(rs.getString(13));
                em.setWorkedTipe(rs.getString(14));
                em.setDependentEconomic(rs.getString(15));
                em.setStudies(rs.getString(16));

             rs.close();
            pt.close();
            cone.closeConection();

        }catch(SQLException e){
            System.out.println("DAOEmpleado - getEmpleado(): "+e.getMessage());
        }
        return em;

    }
    
}
